package com.example.lewisgao.blankactivity;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

/**
 * Created by LewisGao on 2015-06-25 0025.
 */
public class CalculatorActivity extends Activity implements View.OnClickListener {
    private Button btn1;
    private Button btn2;
    private Button btn3;
    private Button btn4;
    private Button btn5;
    private Button btn6;
    private Button btn7;
    private Button btn8;
    private Button btn9;
    private Button btn0;
    private Button btnClear;
    private Button btnDel;
    private Button btnPlus;
    private Button btnMinus;
    private Button btnMultiply;
    private Button btnDivide;
    private Button btnEqual;
    private Button btnPoint;
    private EditText etInput;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.calculator);
        btn0 = (Button) findViewById(R.id.btn_0);
        btn1 = (Button) findViewById(R.id.btn_1);
        btn2 = (Button) findViewById(R.id.btn_2);
        btn3 = (Button) findViewById(R.id.btn_3);
        btn4 = (Button) findViewById(R.id.btn_4);
        btn5 = (Button) findViewById(R.id.btn_5);
        btn6 = (Button) findViewById(R.id.btn_6);
        btn7 = (Button) findViewById(R.id.btn_7);
        btn8 = (Button) findViewById(R.id.btn_8);
        btn9 = (Button) findViewById(R.id.btn_9);
        btnClear = (Button) findViewById(R.id.btn_clear);
        btnDel = (Button) findViewById(R.id.btn_del);
        btnPlus = (Button) findViewById(R.id.btn_plus);
        btnMinus = (Button) findViewById(R.id.btn_minus);
        btnMultiply = (Button) findViewById(R.id.btn_multiply);
        btnDivide = (Button) findViewById(R.id.btn_divide);
        btnEqual = (Button) findViewById(R.id.btn_equal);
        btnPoint = (Button) findViewById(R.id.btn_point);
        etInput = (EditText) findViewById(R.id.et_input);
        btn0.setOnClickListener(this);
        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
        btn3.setOnClickListener(this);
        btn4.setOnClickListener(this);
        btn5.setOnClickListener(this);
        btn6.setOnClickListener(this);
        btn7.setOnClickListener(this);
        btn8.setOnClickListener(this);
        btn9.setOnClickListener(this);
        btnClear.setOnClickListener(this);
        btnDel.setOnClickListener(this);
        btnPlus.setOnClickListener(this);
        btnMinus.setOnClickListener(this);
        btnMultiply.setOnClickListener(this);
        btnDivide.setOnClickListener(this);
        btnEqual.setOnClickListener(this);
        btnPoint.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        String str = etInput.getText().toString();
        switch (view.getId()) {
            case R.id.btn_0:
            case R.id.btn_1:
            case R.id.btn_2:
            case R.id.btn_3:
            case R.id.btn_4:
            case R.id.btn_5:
            case R.id.btn_6:
            case R.id.btn_7:
            case R.id.btn_8:
            case R.id.btn_9:
            case R.id.btn_point:
                etInput.setText(str + ((Button) view).getText());
                break;
            case R.id.btn_plus:
            case R.id.btn_minus:
            case R.id.btn_multiply:
            case R.id.btn_divide:
                etInput.setText(str + " " + ((Button) view).getText() + " ");
                break;
            case R.id.btn_del:
                if (!str.equals("")) {
                    etInput.setText(str.substring(0, str.length() - 1));
                }
            case R.id.btn_clear:
                etInput.setText("");
                break;
            case R.id.btn_equal:
                getResult();
        }
    }


    public void getResult() {
        String exp = etInput.getText().toString();
        if (exp.equals("")) {
            return;
        }
        if (!exp.contains(" ")) {
            return;
        }
        int space = exp.indexOf(' ');//用于搜索空格位置
        String s1 = exp.substring(0, space);//s1用于保存第一个运算数
        String op = exp.substring(space + 1, space + 2);//op用于保存运算符
        String s2 = exp.substring(space + 3);//s2用于保存第二个运算数
        double arg1 = Double.parseDouble(s1);//将运算数从string转换为Single
        double arg2 = Double.parseDouble(s2);
        double r = 0;
        if (op.equals("+")) {
            r = arg1 + arg2;
        } else if (op.equals("-")) {
            r = arg1 - arg2;
        } else if (op.equals("×")) {
            r = arg1 * arg2;
        } else if (op.equals("÷")) {
            if (arg2 == 0) {
                r = 0;
            } else {
                r = arg1 / arg2;
            }
        }
        if (!s1.contains(".") && !s2.contains(".")) {
            int result = (int) r;
            etInput.setText(result + "");
        } else {
            etInput.setText(r + "");
        }
    }
}
